package com.runner.player.state;

import com.runner.entity.CollisionEntity;
import com.runner.observer.Event;
import com.runner.player.Player;

public class FloatState extends PlayerState {

	private float timer;
	public static final float FLOAT_TIME = 1.0f;
	private boolean requestRoll;
	private boolean hitGround;

	public FloatState(Player p) {
		super(p);
	}

	@Override
	public void update(float delta) {
		timer += delta;
		if (timer > FLOAT_TIME) {
			exit();
			player.getPhysics().getBody().setGravityScale(2);
		}
		if(hitGround){
			exit();
			if(requestRoll){
				player.changeState(new RollState(player));
			} else {
				player.changeState(new NormalState(player));
			}
		}
	}

	@Override
	public void newAction(INPUT input) {
		switch (input) {
		case TOUCH_UP:
			player.getPhysics().getBody().setGravityScale(2);
			break;
		case ROLL:
			requestRoll = true;
			break;
		default:
			break;

		}
	}

	@Override
	public void enter() {
		player.getSubject().notify(Event.EVENT_FLOAT);
		player.getPhysics().getBody().setGravityScale(0);
		
		requestRoll=false;
		hitGround=false;
	}

	@Override
	public void exit() {
		
	}

	@Override
	public void collide(CollisionEntity otherBody) {
		if (otherBody.getPhysics().getType().equals(TYPE.GROUND)) {
			hitGround = true;
			player.getSubject().notify(Event.EVENT_PLAYER_HIT_GROUND);
		}
	}

}
